package com.elong.businesstravel.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.umeng.socialize.CustomShareDialog;
import com.elong.businesstravel.base.unionlogin.UnionLoginSucceed;
import com.elong.businesstravel.base.unionlogin.qq.QQUnionLogin;
import com.elong.businesstravel.base.unionlogin.sinaweibo.SinaUnionLogin;
import com.elong.businesstravel.base.unionlogin.weixin.WeChatUnionLogin;
import com.elong.businesstravel.base.utils.AppUserId;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.entitys.MemberLogin;
import com.elong.businesstravel.entitys.UnionLoginInfo;
import com.elong.businesstravel.modules.home.HomeActivity;
import com.elong.businesstravel.modules.login.RegisterSucceedActivity;
import com.elong.businesstravel.modules.login.baseactivity.BaseLoginActivity;
import com.elong.businesstravel.protocol.login.GetYZMRequest;
import com.elong.businesstravel.protocol.login.GetYZMResponse;
import com.elong.businesstravel.protocol.login.MemberCheckLoginRequest;
import com.elong.businesstravel.protocol.login.MemberCheckLoginResponse;
import com.elong.businesstravel.protocol.login.MemberUnionLoginRequest;
import com.elong.businesstravel.protocol.login.MemberUnionLoginResponse;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.onClick.OnClickNetworkStatusListener;

/**
 * 手机验证
 * 
 * @author lenovo
 * 
 */
public class WXEntryActivity extends BaseActivity implements
		IWXAPIEventHandler {

	private WeChatUnionLogin mWeChatUnionLogin;
	private UnionLoginInfo mUnionLoginInfo = new UnionLoginInfo();
	
	private MemberLogin mMemberLogin;

	/** 用于微信联合登陆 */
	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DebugLog.logI("WXEntryActivity onCreate");

		init();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);

		DebugLog.logI("WXEntryActivity onNewIntent");
	}

	@Override
	protected void init() {
		super.init();

		mWeChatUnionLogin = new WeChatUnionLogin(mContext);

		// 微信联合登陆
		api = WXAPIFactory.createWXAPI(this, CustomShareDialog.WX_APPID, false);
		api.handleIntent(getIntent(), this);

		setListener();
	}

	@Override
	protected void setListener() {

		mWeChatUnionLogin.setUnionLoginSucceedListener(new UnionLoginSucceed() {

			@Override
			public void onUnionLoginSucceed(UnionLoginInfo unionLoginInfo) {
				unionLoginInfo.opentype = WeChatUnionLogin.OPEN_TYPE;
				memberUnionLogin(unionLoginInfo);
			}
		});

		super.setListener();
	}

	/**
	 * 联合登陆
	 * 
	 * @param unionLoginInfo
	 */
	private void memberUnionLogin(UnionLoginInfo unionLoginInfo) {
		mUnionLoginInfo = unionLoginInfo;
		NetworkClient.getNetworkClient().PostRequest(
				new MemberUnionLoginRequest(mContext, unionLoginInfo.openid,
						unionLoginInfo.opentype),
				new MemberUnionLoginResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mMemberLogin = memberLogin;
								if ("0".equals(mMemberLogin.status)) {
									if (null != memberLogin) {
										if (!TextUtils
												.isEmpty(memberLogin.uphone)) {
											SaveUserOperate.setLoginPhone(
													mContext,
													memberLogin.uphone);
										}
										if (!TextUtils
												.isEmpty(memberLogin.nickname)) {
											SaveUserOperate.setLoginNickName(
													mContext,
													memberLogin.nickname);
										}
										jumpActivity(1);
									}
								} else {
									HandlerToastUI.getHandlerToastUI(mContext,
											mMemberLogin.msg);
								}

							}
						});

					}
				});
	}

	/**
	 * 跳转界面
	 * 
	 * @param flag
	 *            0:手机号获取验证码地跳转，1:联合登陆跳转
	 */
	private void jumpActivity(int flag) {
		if (1 == flag) {
			int tempUid = 0;
			try {
				tempUid = Integer.parseInt(mMemberLogin.uid);
			} catch (Exception e) {
			}
			if (tempUid > 0) {
				AppUserId.setUserId(mContext, mMemberLogin.uid);
				// 老用户
				// 登陆成功
//				setResult(RESULT_OK);
//				finish();
				goHomeActivity();
			} else {
				// 新用户
				Intent intent = new Intent(mContext,
						RegisterSucceedActivity.class);
				intent.putExtra("flag", flag);
				intent.putExtra("MemberLogin", mMemberLogin);
				intent.putExtra("UnionLoginInfo", mUnionLoginInfo);
				startActivityForResult(intent, 0);
			}
		}
	}
	
	private void goHomeActivity(){
		DebugLog.logI("WXEntryActivity goHomeActivity");
//		Intent intent = new Intent(mContext, HomeActivity.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		intent.putExtra("Activity", "WXEntryActivity");
//		startActivity(intent);
		setResult(RESULT_OK);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case 0: {
			// 填写邀请码返回
			switch (resultCode) {
			case RESULT_OK: {
				// 成功发送邀请码
				setResult(RESULT_OK);
				finish();
				break;
			}
			case RESULT_CANCELED: {
				// 跳过邀请码
				setResult(RESULT_OK);
				finish();
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void onReq(BaseReq arg0) {

	}

	@Override
	public void onResp(BaseResp resp) {
		DebugLog.logI("WXEntryActivity onResp");
		mWeChatUnionLogin.onResp(this, resp);
	}
}
